package com.itheima.info.manager.controller;

import com.itheima.info.manager.domain.Student;
import com.itheima.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    //创建studentService类,把封装好的stu类传递给studentService
    private StudentService studentService = new StudentService();
    //初始化Scanner
    public Scanner sc = new Scanner(System.in);
    public final void start() {
        studentloop:
        while (true){
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choise = sc.next();
            switch (choise){
                case "1":
                    //System.out.println("添加");
                    //添加学生信息,并封装到student类中
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查询");
                    showStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统,再见!");
                    break studentloop;
                default:
                    System.out.println("您输入的有误,请重新输入");
                    break;
            }
        }
    }

    private final void updateStudent() {
        String id = inputStudentId();
        Student stu = inputStudentInfo(id);
        //调用updateStudent方法,判断是否修改成功,返回result值.
        boolean result = studentService.updateStudent(stu);
        if (result){
            System.out.println("已经把学号" + id + "修改");
            System.out.println("修改后的姓名是" + stu.getName() + ",年龄是" + stu.getAge() + ",出生日期是" + stu.getBirthday());
        }else {
            System.out.println("修改失败");
        }
    }

    private final void deleteStudent() {
        String id = inputStudentId();
        //调用deleteStudent方法,判断是否删除成功,返回result结果.
        boolean result = studentService.deleteStudent(id);
            if (result){
                System.out.println("已经把学号" + id + "删除");
            }else {
                System.out.println("删除失败");
            }
    }

    private final void showStudent() {
        //创建Student[]数组,接收findAllStudent返回的数组.
        Student[] students = studentService.findAllStudent();
        //如果返回数组为null,说明数组是空的还没有添加信息.
        if (students == null){
            System.out.println("您还没有添加学生信息,请添加后再查询");
        }else {
            //遍历数组打印数组中不为null的元素.
            System.out.println("学号\t姓名\t年龄\t出生日期");
            for (int i = 0; i < students.length; i++) {
                if (students[i] != null){
                    System.out.println(students[i].getId() + "\t\t" + students[i].getName() + "\t\t" + students[i].getAge() + "\t\t" + students[i].getBirthday());
                }
            }
        }
    }
    public final void addStudent() {
        //创建stu学生对象用于保存学生元素.
        //Student stu = new Student();
        String id;
        //使用while循环包裹,调用isExists方法判断学号是否存在.
        while (true){
            System.out.println("请输入学生学号");
            id = sc.next();
            boolean result = studentService.isExists(id);
            if (result){
                System.out.println("您输入的学号已存在,请重新输入");
            }else {
                break;
            }
        }
        Student stu = inputStudentInfo(id);
        //用result接收boolean值,判断是否添加成功
        boolean result = studentService.addStudent(stu);
        if (result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
    public final String inputStudentId(){
        String id;
        while (true){
            //提示用户输入要修改的学号
            System.out.println("请输入要操作的学号");
            id = sc.next();
            //调用isExists方法,判断学号是否存在.
            boolean result = studentService.isExists(id);
            if (result){
                break;
            }else {
                System.out.println("您要修改的学生学号不存在,请重新输入");
            }
        }
       return id;
    }
    public abstract Student inputStudentInfo(String id);
}
